"""
Case Type   : DESC/DESCRIBE
Case Name   : desc查询mot表结构
Create At   : 2022-10-09
Owner       : @zou_jialiang050
Description :
    1.设置guc参数
    2.创建兼容B库
    3.B库下建表
    4.desc分析表结构
    5.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.返回表结构
    5.成功
History     : 
"""

import os
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class DESC(unittest.TestCase):
    def setUp(self):
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.com = Common()
        self.constant = Constant()
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.database = 'd_compatibility_desc_0010'
        self.table = 't_compatibility_desc_0010'

    def test_desc(self):
        text1 = 'step1:设置guc参数 expect:成功'
        self.param = self.com.show_param('enable_incremental_checkpoint')
        guc_res1 = self.pri_sh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            'enable_incremental_checkpoint=off')
        guc_res2 = self.pri_sh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            'enable_mot_server=on')
        self.log.info(guc_res1)
        self.log.info(guc_res2)
        self.assertTrue(guc_res1, '执行失败' + text1)
        self.assertTrue(guc_res2, '执行失败' + text1)
        result = self.pri_sh.restart_db_cluster(True)
        self.log.info(result)
        self.assertTrue(result, '执行失败' + text1)

        text2 = 'step2:创建兼容B库 expect:成功'
        self.log.info(text2)
        create_database = self.pri_sh.execut_db_sql(
            f'''drop database if exists {self.database};
            create database {self.database} dbcompatibility='B';''')
        self.log.info(create_database)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, create_database,
                      '执行失败' + text2)

        text3 = 'step3:B库下建表 expect:成功'
        self.log.info(text3)
        create_table = self.pri_sh.execut_db_sql(
            f'''drop foreign table if exists {self.table};
            create foreign table {self.table}(c_int int default null,c_test 
            varchar(10) default ' ');''', dbname=self.database)
        self.log.info(create_table)
        self.assertIn(self.constant.CREATE_FOREIGN_SUCCESS_MSG, create_table,
                      '执行失败' + text3)

        text4 = 'step4:desc分析表结构 expect:返回表结构'
        self.log.info(text4)
        desc_table = self.pri_sh.execut_db_sql(
            f'''desc {self.table};''', dbname=self.database)
        self.log.info(desc_table)
        self.assertIn('c_int  | integer               | YES  |     | NULL',
                      desc_table, '执行失败' + text4)
        self.assertIn("c_test | character varying(10) | YES  |     | ' ':"
                      ":character varying", desc_table, '执行失败' + text4)

    def tearDown(self):
        text5 = 'step5:清理环境 expect:成功'
        self.log.info(text5)
        clean_environment = self.pri_sh.execut_db_sql(
            f'''drop foreign table {self.table};''', dbname=self.database)
        self.log.info(clean_environment)
        clean_environment1 = self.pri_sh.execut_db_sql(
            f'''drop database {self.database};''')
        self.log.info(clean_environment1)
        guc_res = self.pri_sh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'enable_incremental_checkpoint={self.param}')
        self.log.info(guc_res)
        self.assertTrue(guc_res, '执行失败' + text5)
        result = self.pri_sh.restart_db_cluster(True)
        self.log.info(result)
        self.assertTrue(result, '执行失败' + text5)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, clean_environment,
                      '执行失败' + text5)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, clean_environment1,
                      '执行失败' + text5)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')

